using System.Collections.Generic;
using UnityEngine;

public class TasksController : MonoBehaviour
{
    public static TasksController instance;

    public TASKMODE taskmode;

    public int chanllengeid;

    public int challengemode;

    public GameLevel curgamelevel;

    public int curlevelnum;

    public List<int> TaskIDList;

    public List<bool> TaskIsSuccess;

    public List<LevelStar> StarList;

    private int curtaskindex;

    public BaseTask CurTask;

    public FightingTask fightingtask;

    public TakeoffTask takeofftask;

    public LandingTask landingtask;

    public FlyToTask flytotask;

    public SurvivalTask survivaltask;

    public HideTask hidetask;

    public GuideTask guidetask;

    public SurvivalChanllenge survivalchanllenge;

    public TeamDeathmatch teamdeathmatch;

    public DefendTheBase defendthebase;

    public FreeForAll freeforall;

    public bool is_gameover;

    public bool hide_find;

    public bool speed_rate_flag;

    public float alltime;

    public int curwave;

    private bool initobjflag;

    private bool gamesucc;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        curlevelnum = GlobalInf.CurrentLevel;
        taskmode = GlobalInf.gamemode;
        chanllengeid = GlobalInf.chanllengeid;
        challengemode = GlobalInf.chanllenge_mode;
    }

    private void Start()
    {
        curgamelevel = DataController.instance.DataLevel[GlobalInf.CurrentLevel];
        for (int i = 0; i < 3; i++)
        {
            TaskIDList[i] = DataController.instance.DataStep[curgamelevel.gamestepid[i]].steptype;
        }
        for (int j = 0; j < 3; j++)
        {
            StarList[j] = DataController.instance.DataLevelstar[curgamelevel.starid[j]];
        }
        initobjflag = false;
    }

    private void Update()
    {
        if (is_gameover || !GlobalInf.loading_over)
        {
            if (!initobjflag)
            {
                initobjflag = true;
                if (GlobalInf.gamefirst)
                {
                    guide_update();
                }
                else if (taskmode == TASKMODE.level)
                {
                    level_update();
                }
                else
                {
                    chanllenge_update();
                }
            }
            return;
        }
        if (GlobalInf.gamefirst)
        {
            guide_update();
            return;
        }
        switch (taskmode)
        {
            case TASKMODE.level:
                level_update();
                break;
            case TASKMODE.chanllenge:
                chanllenge_update();
                break;
        }
    }

    public void guide_update()
    {
        if (CurTask == null)
        {
            CurTask = guidetask;
            CurTask.MyStart();
            return;
        }
        alltime += Time.deltaTime;
        CurTask.MyUpdate();
        if (CurTask.FinishState)
        {
            plane_smothfollow.instance.cam_move.diedmove();
            gameover(issucces: true);
        }
    }

    public void chanllenge_update()
    {
        if (CurTask == null)
        {
            changechanllenge();
            return;
        }
        alltime += Time.deltaTime;
        CurTask.MyUpdate();
        if (CurTask.FinishState)
        {
            plane_smothfollow.instance.cam_move.diedmove();
            gameover(issucces: true);
        }
    }

    public void level_update()
    {
        if (CurTask == null)
        {
            ChangeTask();
            Creat_obj_res();
            return;
        }
        alltime += Time.deltaTime;
        CurTask.MyUpdate();
        if (CurTask.FinishState)
        {
            TaskIsSuccess[curtaskindex - 1] = CurTask.isSuccess;
            if (curtaskindex < 3)
            {
                ChangeTask();
                return;
            }
            plane_smothfollow.instance.cam_move.diedmove();
            gameover(issucces: true);
        }
    }

    public void Creat_obj_res()
    {
        GameStep gameStep = new GameStep();
        for (int i = 0; i < 3; i++)
        {
            gameStep = DataController.instance.DataStep[curgamelevel.gamestepid[i]];
            for (int j = 0; j < gameStep.enemy_id_num.Count; j++)
            {
                AIPool.instance.creat_AI(gameStep.enemy_id_num[j]);
            }
            for (int k = 0; k < gameStep.fri_id_num.Count; k++)
            {
                FriendPool.instance.creat_AI_Fri(gameStep.fri_id_num[k]);
            }
            AIPool.instance.DisableAllAI();
            FriendPool.instance.DisableAllFri();
        }
    }

    public void ChangeTask()
    {
        if (CurTask != null)
        {
            CurTask.MyEnd();
        }
        switch (TaskIDList[curtaskindex])
        {
            case 0:
                CurTask = takeofftask;
                break;
            case 1:
                CurTask = fightingtask;
                break;
            case 2:
                CurTask = landingtask;
                break;
            case 3:
                CurTask = flytotask;
                break;
            case 4:
                CurTask = survivaltask;
                break;
            case 5:
                CurTask = hidetask;
                break;
        }
        CurTask.stepid = curgamelevel.gamestepid[curtaskindex];
        CurTask.MyStart();
        curtaskindex++;
    }

    public void changechanllenge()
    {
        if (CurTask != null)
        {
            CurTask.MyEnd();
        }
        GlobalInf.isInternalInjury = false;
        switch (chanllengeid)
        {
            case 6:
                CurTask = survivalchanllenge;
                break;
            case 7:
                CurTask = teamdeathmatch;
                break;
            case 8:
                CurTask = defendthebase;
                break;
            case 9:
                CurTask = freeforall;
                GlobalInf.isInternalInjury = true;
                break;
        }
        CurTask.MyStart();
    }

    public void gameover(bool issucces)
    {
        CurTask.MyEnd();
        MonoBehaviour.print("game over");
        PlaneMoveController.instance.isdied = true;
        is_gameover = true;
        GlobalInf.gameover = true;
        MonoInstance<AudioController>.instance.stopgamemusic();
        gamesucc = issucces;
        Invoke("UIgameover", 3.5f);
    }

    public void gameover_relive()
    {
        CurTask.MyEnd();
        PlaneMoveController.instance.isdied = true;
        is_gameover = true;
        GlobalInf.gameover = true;
        MonoInstance<AudioController>.instance.stopgamemusic();
        gamesucc = false;
        UIgameover();
    }


    public void UIgameover()
    {
        if (!AdManager1.instance.ShieldAddress)
        {
            CheckTouch.Show();
        }
        else
        {
            AdManager.single.ShowNative();
        }
        UIgameover2();
    }

    public void UIgameover2()
    {

        if (GlobalInf.gamemode == TASKMODE.level)
        {
            GameUIController.instance.enable_resultui_level(gamesucc);
            return;
        }
        switch (GlobalInf.chanllengeid)
        {
            case 6:
                GameUIController.instance.enable_result_sur();
                break;
            case 7:
                GameUIController.instance.enable_result_tdm();
                break;
            case 8:
                GameUIController.instance.enable_result_dtb();
                break;
            case 9:
                GameUIController.instance.enable_result_ffa();
                break;
        }
    }

    public int GetTasknum()
    {
        return curtaskindex;
    }

    private void OnDestroy()
    {
        if (instance != null)
        {
            instance = null;
        }
    }

    public bool[] check_star()
    {
        bool[] array = new bool[3];
        for (int i = 0; i < 3; i++)
        {
            array[i] = onestar(StarList[i]);
        }
        return array;
    }

    public bool onestar(LevelStar ls)
    {
        switch (ls.type)
        {
            case 1:
                if (TaskIsSuccess[2])
                {
                    return true;
                }
                break;
            case 2:
                if (plane_smothfollow.instance.GetHPpercent() > (float)ls.sontype / 100f)
                {
                    return true;
                }
                break;
            case 3:
                return AIPool.instance.check_list_num(ls.sontype);
            case 4:
                return FriendPool.instance.check_list_num(ls.sontype);
            case 5:
                if (alltime <= (float)ls.sontype)
                {
                    return true;
                }
                break;
            case 6:
                if (!hide_find)
                {
                    return true;
                }
                break;
            case 7:
                if (speed_rate_flag)
                {
                    return true;
                }
                break;
        }
        return false;
    }
}
